package com.chuanyang.driverh.mainmy.fragment;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseFragment;
import com.chuanyang.driverh.base.OnCustomListener;
import com.chuanyang.driverh.bean.IdentifyCarBean;
import com.chuanyang.driverh.mainmy.AddLinesActivity;
import com.chuanyang.driverh.mainmy.ShipIdentifyActivity;
import com.chuanyang.driverh.mainmy.TranslateCompanyActivity;
import com.chuanyang.driverh.mainmy.adapter.LinesManageAdapter;
import com.chuanyang.driverh.responseresult.LinesManageData;
import com.chuanyang.driverh.responseresult.LinesManageResult;
import com.chuanyang.driverh.responseresult.LogisticsCompanyData;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.eventbus.ReUpdateLinesEvent;
import com.chuanyang.driverh.utils.eventbus.SelectCompanyEvent;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;
import com.chuanyang.driverh.utils.widget.CustomMeasureHeightListView;
import com.chuanyang.driverh.utils.widget.MyViewPager;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import cn.qqtheme.framework.picker.DatePicker;
import okhttp3.Call;

public class IdentifyShipFragment extends BaseFragment {
    private TextView tv_next;
    private TextView tv_shipidentify_addline;
    private ListView lv_identify_addline;
    private EditText et_ship_name;
    private EditText et_ship_type;
    private TextView et_ship_mmis;
    private TextView et_ship_phone;
    private TextView et_ship_idcode;
    private EditText et_ship_helpcode;
    private EditText et_ship_weiht;
    private EditText et_ship_wide;
    private EditText et_ship_long;
    private CustomMeasureHeightListView lv_shipidentify_addline;
    private TextView tv_ship_date;
//    private DatePickerDialog dateDialog;
    private EditText et_ship_maxload;
    private EditText et_ship_whole;
    private EditText et_ship_cangkou;
    private EditText et_ship_cklong;
    private EditText et_ship_ckwide;
    private TextView et_userinfo_linkcompany;
    private EditText et_ship_rongji;
    private ArrayList linesList;
    private LinesManageAdapter myAdapter;
    private MyViewPager pager;
    private IdentifyCarBean identifyCarBean;
    private boolean ifCarPerson;//如果为true表示是个人认证加车辆认证   false表示为车辆认证
//    private HashMap params;

    private LogisticsCompanyData logisticsCompanyData;
    @Override
    protected int getLayoutResId() {
        return R.layout.my_frag_identifyship;
    }
    @Override
    protected void initView(View view) {

        tv_next = (TextView) getActivity().findViewById(R.id.tv_shipidentify_next);//下一步
        tv_shipidentify_addline = (TextView) rootView.findViewById(R.id.tv_shipidentify_addline);//添加常用路线
        lv_identify_addline = (ListView) rootView.findViewById(R.id.lv_identify_addline);//显示船队路线
        //船名
        et_ship_name = (EditText) rootView.findViewById(R.id.et_ship_name);
        //船只类型
        et_ship_type = (EditText) rootView.findViewById(R.id.et_ship_type);
        //长
        et_ship_long = (EditText) rootView.findViewById(R.id.et_ship_long);
        //宽
        et_ship_wide = (EditText) rootView.findViewById(R.id.et_ship_wide);
        //最大吨位
        et_ship_maxload = (EditText) rootView.findViewById(R.id.et_ship_maxload);
        //最小吨位
        et_ship_weiht = (EditText) rootView.findViewById(R.id.et_ship_weiht);
        //满载吃水
        et_ship_whole = (EditText) rootView.findViewById(R.id.et_ship_whole);
        //舱口数
        et_ship_cangkou = (EditText) rootView.findViewById(R.id.et_ship_cangkou);
        //容积
        et_ship_rongji = (EditText) rootView.findViewById(R.id.et_ship_rongji);
        //船口尺寸长
        et_ship_cklong = (EditText) rootView.findViewById(R.id.et_ship_cklong);
        //船口尺寸宽
        et_ship_ckwide = (EditText) rootView.findViewById(R.id.et_ship_ckwide);
        //所属公司
        et_userinfo_linkcompany = (TextView) rootView.findViewById(R.id.et_userinfo_linkcompany);
        //助记号
        et_ship_helpcode = (EditText) rootView.findViewById(R.id.et_ship_helpcode);

        //MMIS
        et_ship_mmis = (TextView) rootView.findViewById(R.id.et_ship_mmis);
        //日期
        tv_ship_date = (TextView) rootView.findViewById(R.id.tv_ship_date);
//        //年
//        et_ship_year = (TextView) rootView.findViewById(R.id.et_ship_year);
//        //月
//        et_ship_month = (TextView) rootView.findViewById(R.id.et_ship_month);
//        //日
//        et_ship_date = (TextView) rootView.findViewById(R.id.et_ship_date);
        //身份证号
        et_ship_idcode = (TextView) rootView.findViewById(R.id.et_ship_idcode);
        //联系电话
        et_ship_phone = (TextView) rootView.findViewById(R.id.et_ship_phone);
        //添加路线
        tv_shipidentify_addline = (TextView) rootView.findViewById(R.id.tv_shipidentify_addline);
        //显示路线的列表
        lv_shipidentify_addline = (CustomMeasureHeightListView) rootView.findViewById(R.id.lv_identify_addline);


    }

    @Override
    protected void initData() {
        if (linesList == null) {
            linesList = new ArrayList();
        }
        if (myAdapter == null) {
            myAdapter = new LinesManageAdapter(getActivity(), linesList);
        }
        lv_identify_addline.setAdapter(myAdapter);


        if (getActivity() instanceof ShipIdentifyActivity) {
            identifyCarBean = ((ShipIdentifyActivity) getActivity()).getIdentifyCarBean();
            ifCarPerson = false;
            pager = ((ShipIdentifyActivity) getActivity()).getPager();

        } else {
//            ifCarPerson = true;
//            identifyCarBean = ((ShipPersonIdentifyActivity) getActivity()).getIdentifyCarBean();
//            pager = ((ShipPersonIdentifyActivity) getActivity()).getPager();
        }
        getLines();
    }



    @Override
    protected void initListener() {
        tv_next.setOnClickListener(this);
        tv_shipidentify_addline.setOnClickListener(this);
        tv_ship_date.setOnClickListener(this);
        et_userinfo_linkcompany.setOnClickListener(this);
        myAdapter.setOnCustomListener(new OnCustomListener() {
            @Override
            public void onCustomerListener(View v, int position) {
                switch (v.getId()) {
                    case R.id.iv_lines_delete:
                        deleteLines(myAdapter.getItem(position));
                        break;
                }
            }
        });

    }


    private void deleteLines(final LinesManageData.DataBean bean) {
        DialogUtil.showLoadingDialog(getActivity(),"删除路线...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("id", bean.getId() + "");
        MyHttpUtil.deleteLines(getActivity(),params, new MyCallback<LinesManageResult>(getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(LinesManageResult result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    myAdapter.remove(bean);
                    lv_identify_addline.requestLayout();
                    PrintUtil.toast(getActivity(),"路线删除成功");
                }else{
                    PrintUtil.toast(getActivity(),result.getMsg());
                }
            }
        });


    }
    private void getLines() {
        DialogUtil.showLoadingDialog(getActivity(),"路线...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("driverId", SharedPrefUtil.getUserId());
        MyHttpUtil.getLines(getActivity(),params, new MyCallback<LinesManageResult>(getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(LinesManageResult result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    myAdapter.clear();
                    myAdapter.addAll(result.getData().getData());
                }else{
                    PrintUtil.toast(getActivity(),result.getMsg());
                }
            }
        });
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_shipidentify_next:
                if (checkall()) {
                    setEntity();
                    if (ifCarPerson) {
                        pager.setCurrentItem(2);
                    }
                    else {
                        pager.setCurrentItem(1);

                    }
                }
                break;
            case R.id.tv_shipidentify_addline:
                startActivity(AddLinesActivity.class);
                break;
            case R.id.tv_ship_date:
                onYearMonthDayPicker(tv_ship_date);

                break;
            case R.id.et_userinfo_linkcompany:
                startActivity(TranslateCompanyActivity.class);

                break;

        }
    }



    public void onYearMonthDayPicker(final View view) {
        final DatePicker picker = new DatePicker(getActivity());
        Calendar c=Calendar.getInstance();
       int year= c.get(Calendar.YEAR);
       int month= c.get(Calendar.MONTH)+1;
       int day= c.get(Calendar.DAY_OF_MONTH);

        picker.setTopPadding(6);

//        picker.setRangeStart(2000, 12, 31);
//        picker.setRangeEnd(2111, 12, 31);
//        picker.setSelectedItem(2050, 10, 14);

        picker.setRangeStart(year-30, 1, 1);
        picker.setRangeEnd(year, month,day);
        picker.setSelectedItem(year-30, 1, 1);


        picker.setOnDatePickListener(new DatePicker.OnYearMonthDayPickListener() {
            @Override
            public void onDatePicked(String year, String month, String day) {
                ((TextView)view).setText(year + "-" + month + "-" + day);
            }
        });
        picker.setOnWheelListener(new DatePicker.OnWheelListener() {
            @Override
            public void onYearWheeled(int index, String year) {
                picker.setTitleText(year + "-" + picker.getSelectedMonth() + "-" + picker.getSelectedDay());
            }

            @Override
            public void onMonthWheeled(int index, String month) {
                picker.setTitleText(picker.getSelectedYear() + "-" + month + "-" + picker.getSelectedDay());
            }

            @Override
            public void onDayWheeled(int index, String day) {
                picker.setTitleText(picker.getSelectedYear() + "-" + picker.getSelectedMonth() + "-" + day);
            }
        });
        picker.show();
    }

    private void setEntity() {
        identifyCarBean.setShipName(et_ship_name.getText().toString().trim());//船名
        identifyCarBean.setCarType(et_ship_type.getText().toString().trim());//船只类型
        identifyCarBean.setCarStandard(et_ship_long.getText().toString().trim()+"*"+et_ship_wide.getText().toString().trim());
        identifyCarBean.setCapacity(et_ship_maxload.getText().toString().trim());
        identifyCarBean.setActualCapacity(et_ship_weiht.getText().toString().trim());
        identifyCarBean.setDraft(et_ship_whole.getText().toString().trim());
        identifyCarBean.setHatchNum(et_ship_cangkou.getText().toString().trim());
        identifyCarBean.setHatchVolume(et_ship_rongji.getText().toString().trim());
        identifyCarBean.setHatchSize(et_ship_cklong.getText().toString().trim()+"*"+et_ship_ckwide.getText().toString().trim());
        identifyCarBean.setPlateNumber(et_ship_helpcode.getText().toString().trim());//助记号
        identifyCarBean.setFrameNumber(et_ship_mmis.getText().toString().trim());//mmis号
        identifyCarBean.setShipCreateDate(tv_ship_date.getText().toString());//船出厂日期
        identifyCarBean.setDriverIdentity(et_ship_idcode.getText().toString().trim());
        identifyCarBean.setDriverContact(et_ship_phone.getText().toString().trim());
        if(logisticsCompanyData!=null)
        {
            identifyCarBean.setCompanyId(logisticsCompanyData.getLogisticsCompanyId());
        }

    }

    private boolean checkall() {
        if (TextUtils.isEmpty(et_ship_name.getText().toString())) {
           PrintUtil.toast(getActivity(),"请填写船名");
            return false;
        } else if (TextUtils.isEmpty(et_ship_type.getText().toString())) {
            PrintUtil.toast(getActivity(),"请填写船只类型");
            return false;
        }
        else if (TextUtils.isEmpty(et_ship_long.getText().toString())) {
            PrintUtil.toast(getActivity(),"请填写船只长度");
            return false;
        }
        else if (TextUtils.isEmpty(et_ship_wide.getText().toString())) {
            PrintUtil.toast(getActivity(),"请填写船只宽度");
            return false;
        }
        else if (TextUtils.isEmpty(et_ship_maxload.getText().toString())) {
            PrintUtil.toast(getActivity(),"请填写船只最大吨位");
            return false;
        }
        else if (TextUtils.isEmpty(et_ship_weiht.getText().toString())) {
            PrintUtil.toast(getActivity(),"请填写船只最小吨位");
            return false;
        }
        else if (TextUtils.isEmpty(et_ship_whole.getText().toString())) {
            PrintUtil.toast(getActivity(),"请填写船只满载吃水量");
            return false;
        }
        else if (TextUtils.isEmpty(et_ship_cangkou.getText().toString())) {
            PrintUtil.toast(getActivity(),"请填写船只舱口数");
            return false;
        }
        else if (TextUtils.isEmpty(et_ship_rongji.getText().toString())) {
            PrintUtil.toast(getActivity(),"请填写船只的容量");
            return false;
        }
        else if (TextUtils.isEmpty(et_ship_cklong.getText().toString())) {
            PrintUtil.toast(getActivity(),"请填写船只舱口长度");
            return false;
        }
        else if (TextUtils.isEmpty(et_ship_ckwide.getText().toString())) {
            PrintUtil.toast(getActivity(),"请填写船只舱口宽度");
            return false;
        }
        else if (TextUtils.isEmpty(et_ship_helpcode.getText().toString())) {
            PrintUtil.toast(getActivity(),"请填写船只助记号");
            return false;
        } else if (TextUtils.isEmpty(et_ship_mmis.getText().toString())) {
            PrintUtil.toast(getActivity(),"请填写船只MMIS号");
            return false;
        } else if (TextUtils.isEmpty(et_ship_weiht.getText().toString())) {
            PrintUtil.toast(getActivity(),"请填写车辆运力");
            return false;
        }
        else if (TextUtils.isEmpty(et_ship_long.getText().toString())) {
            PrintUtil.toast(getActivity(),"请填写船只长度");
            return false;
        }
        else if (tv_ship_date.getText().toString().equals("点击选择")) {
            PrintUtil.toast(getActivity(),"请选择日期");
            return false;
        }
//        else if (TextUtils.isEmpty(et_ship_year.getText().toString())) {
//            showMsg("请填写船只出厂年份");
//            return false;
//     }
//        else if (TextUtils.isEmpty(et_ship_month.getText().toString())) {
//            showMsg("请填写船只出厂月份");
//            return false;
//        }
//        else if (TextUtils.isEmpty(et_ship_date.getText().toString())) {
//            showMsg("请填写船只出厂日期");
//            return false;
//        }
        else if (TextUtils.isEmpty(et_ship_idcode.getText().toString())) {
            PrintUtil.toast(getActivity(),"请填写身份证号");
            return false;
        }
        else if (TextUtils.isEmpty(et_ship_phone.getText().toString())) {
            PrintUtil.toast(getActivity(),"请填写联系方式");
            return false;
        }
        else if (myAdapter.getCount() <= 0)
        {
            PrintUtil.toast(getActivity(),"请至少添加一条路线");
            return false;
        }

        return true;
    }

    public void onEventMainThread(ReUpdateLinesEvent event) {
        if (event.isUpdate()) {
            getLines();
        }

    }

    public void onEventMainThread(SelectCompanyEvent event) {

        if (event.getData() != null) {
            logisticsCompanyData = event.getData();
            et_userinfo_linkcompany.setText(logisticsCompanyData.getLogisticsCompanyName());
        }
    }

}


